package com.example.mvvmtemplate.utli.permission;

import android.content.pm.PackageManager;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import java.util.ArrayList;
import java.util.List;

public class InvisibleFragment extends Fragment {

    private PermissionCallback callback;
    public interface PermissionCallback{
        void onPermissionCallback(boolean allGranted, List<String> deniedList);
    }

    public void requestNow(PermissionCallback cb,String... permissions){
        callback = cb;
        requestPermissions(permissions, 1);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == 1) {
            //被用户拒绝的权限列表
            ArrayList<String> deniedList = new ArrayList<>();
            int index = 0;
            for (int result: grantResults){
                if (result != PackageManager.PERMISSION_GRANTED){
                    deniedList.add(permissions[index]);
                }
                index++;
            }
            boolean allGranted = deniedList.isEmpty();
            if (callback != null){
                callback.onPermissionCallback(allGranted,deniedList);
            }
        }
    }
}
